package com.mystudy.jcu.countDownLatch;

import java.util.concurrent.CountDownLatch;

/**
 * TODO: {@link CountDownLatch}测试
 * 第二种用法： 在串行化之前的可并行化的任务可以用这个进行并行化。允许一个或者多个线程在某一个线程执行某一个任务执行之前进行等待操作。
 * @author acy
 * @since 2019/06/17 17:09
 */
public class CountDownLatchTest2 {
	public static void main(String[] args) throws InterruptedException {
		CountDownLatch downLatch = new CountDownLatch(1);

		new Thread(()->{
			System.out.println("do some initial working");
			try {
				Thread.sleep(10000);
				downLatch.await();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}).start();

		new Thread(()->{
			System.out.println("asyn prepare for some date");
			try {
				Thread.sleep(1000);
				System.out.println("data prepare work done");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}finally {
				downLatch.countDown();
			}
		}).start();
		new Thread(()->{
			try {
				System.out.println("await");
				//此时的downLatch只有一个线程，但是可以让多个线程进行等待
				downLatch.await();
				System.out.println("release");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}).start();
		Thread.currentThread().join();
	}
}
